#ifndef VECTOR
#define VECTOR

template <typename T>
class Vector{
private:
	T*		m_data;			//范型指针
	int 	m_capacity;		//数组的容量
	int 	m_size;			//实际元素的个数
public:	
	typedef T* iterator;

	iterator begin() { return this->m_data; }
	iterator end() { return this->m_data + this->m_size; }

	Vector(int n, const T& value)
		:m_size(n),
		m_capacity(n)
	{
		this->m_data = new T[this->m_capacity]();
		for(int i = 0; i < this->m_size; i++){
			this->m_data[i] = value;
		}
	}
	Vector(const Vector& x)
		:m_size(x.m_size),
		m_capacity(x.m_capacity)
	{
		this->m_data =  new T[this->m_capacity]();
		for(int i = 0; i < this->m_size; i++){
			this->m_data[i] = x.m_data[i];
		}
	}

	Vector(int maxSize = 5);
	~Vector();

	void push_back(const T& value);
	void pop_back();

	int size() const { return this->m_size; }

	T& operator [](int index);
};

//构造函数
template <typename T>
Vector<T>::Vector(int maxSize)
	:m_size(0),
	m_capacity(maxSize)
{
	//申请空间
	this->m_data = new T[this->m_capacity]();
}

template <typename T>
Vector<T>::~Vector()
{
	if(this->m_data)
		delete [] this->m_data;
}

template <typename T>
void Vector<T>::push_back(const T& value)
{
	//自动拓展空间
	if(this->m_size >= this->m_capacity){
		//扩容空间
		this->m_capacity += this->m_capacity / 2;
		//申请新空间
		T* _tmp = new T[this->m_capacity]();
		//拷贝旧空间的内容到新空间
		//memcpy
		for(int i = 0; i < this->m_size; i++){
			_tmp[i] = this->m_data[i];
		}
		//释放旧空间
		delete [] this->m_data;
		//指向新空间
		this->m_data  = _tmp;
	}
	this->m_data[m_size++] = value;
}

template <typename T>
void Vector<T>::pop_back()
{
	m_size--;
}

template <typename T>
T& Vector<T>::operator [] (int index)
{
	return this->m_data[index];
}


#endif
